/**
 * Created by sunrui on 2018/7/5.
 */
import MYCONF from '../myconf'
import Vue from 'vue'
import DateTimeHelper from '../util/datetime-helper'

export default {
  namespaced: true,

  state: {
    historyTotalNum: 0,
    onlineTypeArr: [0, 0, 0, 0, 0, 0],
    activeArr: [0, 0],
    carTypeArr: [],
    warnArr: [0, 0],
    energyArr: [0, 0, 0, 0, 0, 0]
  },

  getters: {
    selectedTime(state, getters, rootState, rootGetters) {
      const historyTime = rootGetters.hotMap_time;
      return String(historyTime.year) +
        (historyTime.month > 9 ? String(historyTime.month) : '0' + String(historyTime.month)) +
        (historyTime.day > 9 ? String(historyTime.day) : '0' + String(historyTime.day));
    },
    historyTotalNum: state => state.historyTotalNum,
    onlineTypeArr: state => state.onlineTypeArr,
    activeArr: state => state.activeArr,
    carTypeArr: state => state.carTypeArr,
    warnArr: state => state.warnArr,
    energyArr: state => state.energyArr
  },

  mutations: {
    setHistoryTotalNum(state, payload) {
      state.historyTotalNum = payload;
    },
    setOnlineTypeArr(state, payload) {
      state.onlineTypeArr = payload;
    },
    setActiveArr(state, payload) {
      state.activeArr = payload;
    },
    setCarTypeArr(state, payload) {
      state.carTypeArr = payload;
    },
    setWarnArr(state, payload) {
      state.warnArr = payload;
    },
    setEnergyArr(state, payload) {
      state.energyArr = payload;
    }
  },

  actions: {
    requsetHistoryTotalNum(context, date) {
      Vue.http.get(MYCONF.INDEX_HISTORY_TOTAL_NUM_URL, {
        params: {
          dateCur: date.dateCur,
          dateNext: date.dateNext
        }
      })
        .then(response => {
          response.json().then(json => {
            if (json.state === 1) {
              context.commit('setHistoryTotalNum', json.data.carNum);
            } else {
              console.error("history data request error", json.status, json.resultStr);
            }
          });
        }, response => {
          console.error("history data request error", response.status)
        });
    },
    requsetOnlineTypeArr(context, data) {
      Vue.http.get(MYCONF.INDEX_ONLINE_TYPE_ARR_URL, {
        params: {
          dateCur: data.dateCur,
          dateNext: data.dateNext
        }
      })
        .then(response => {
          response.json().then(json => {
            if (json.state === 1) {
             /* console.log("上线情况:",json.data)*/
              context.commit('setOnlineTypeArr', [json.data.taxiRatio, json.data.busRatio, json.data.bcRatio, json.data.whRatio, json.data.phRatio, json.data.kcRatio]);
            } else {
              console.error("online data request error", json.status, json.resultStr);
            }

          });
        }, response => {
          console.error("online data request error", response.status)
        });
    },
    requsetActiveArr(context, data) {
      Vue.http.get(MYCONF.INDEX_ACTIVE_ARR_URL, {
        params: {
          companyId: data.companyId,
          dateCur: data.dateCur,
          dateNext: data.dateNext
        }
      })
        .then(response => {
          response.json().then(json => {
            if (json.state === 1) {
              context.commit('setActiveArr', [json.data.carActive, json.data.carNotActive]);
            } else {
              console.error("active data request error", json.status, json.resultStr);
            }
          });
        }, response => {
          console.error("active data request error", response.status)
        });
    },
    requsetCarTypeArr(context, date) {
      Vue.http.get(MYCONF.INDEX_CAR_ARR_URL, {
        params: {date}
      })
        .then(response => {
          response.json().then(json => {
            if (Array.isArray(json.result)) {
              const newArr = [];
              const nameArr = ["出租车", "公交车", "班线客车", "旅游包车", "危险货车", "普通货车"];
              let typeIndex = 0;
              json.result.forEach((item) => {
                if ((item.car_type_id - 1) == typeIndex) {
                  newArr.push({
                    name: nameArr[typeIndex],
                    num: item.num,
                    percent: item.percent
                  });
                  typeIndex++;
                } else {
                  newArr.push({
                    name: nameArr[typeIndex],
                    num: 0,
                    percent: 0
                  });
                  typeIndex++;
                  newArr.push({
                    name: nameArr[typeIndex],
                    num: item.num,
                    percent: item.percent
                  });
                  typeIndex++;
                }
              });
              context.commit('setCarTypeArr', newArr);
            }
            // context.commit('setCarTypeArr', json);
          });
        }, response => {
          console.error("car data request error", response.status)
        });
    },
    requsetWarnArr(context, data) {
      let curDate = DateTimeHelper.getCurDate();
      Vue.http.get(MYCONF.INDEX_WARN_NUM_URL, {
        params: {
          date: data.date,
          companyId: data.companyId,
          curDate:curDate
        }
      })
        .then(response => {
          response.json().then(json => {
            if (json.nums) context.commit('setWarnArr', json.nums);
          });
        }, response => {
          console.error("warn data request error", response.status)
        });
    },
    requsetEnergyArr(context, date) {
      Vue.http.get(MYCONF.INDEX_ENERGY_ARR_URL, {
        params: {date}
      })
        .then(response => {
          response.json().then(json => {
            context.commit('setEnergyArr', json);
          });
        }, response => {
          console.error("energy data request error", response.status)
        });
    }
  }
}
